/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment_cp3120.manager.editflights;

import assignment_cp3120.dataObjects.AllFlights;
import assignment_cp3120.dataObjects.AllLocations;
import assignment_cp3120.dataObjects.Flight;
import assignment_cp3120.ucm.UC2AL;
import assignment_cp3120.ucm.UCEditFlights;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author Administrator
 */
public class EditFlightsView extends JPanel {
    private JButton editFlightBtn;
    private JComboBox editFlightsCBox;

    private EditFlightsModel model;

    private GridLayout layout;
    private JPanel depPanel;
    private JPanel destPanel;
    private JPanel depD_TPanel;
    private JPanel capacityPanel;
    private JPanel flightPricePanel;
    private JPanel buttonPanel;

    private JComboBox depCBox;
    private JLabel depLabel;

    private JComboBox destCBox;
    private JLabel destLabel;

    private JTextField priceField;

    private JLabel priceLabel;

    private JPanel flightContainer;

    private JComboBox dayCBox;
    private JComboBox monthCBox;
    private JComboBox yearCBox;
    private JComboBox dhoursCBox;
    private JComboBox dminutesCBox;
    private JComboBox ahoursCBox;
    private JComboBox aminutesCBox;

    private JPanel dateContainer;
    private JPanel depTimeContainer;
    private JPanel arrTPanel;
    private JPanel arrTimePanel;

    private JTextField capacityField;
    private JLabel capacityLabel;
    private JPanel capacityContainer;

    private final int DESCIPTION_LENGTH = 2;
    
    public EditFlightsView(EditFlightsModel model) {
        this.model = model;

        createPanel();
    }
    public Flight getSelectedFlight() {
        return AllFlights.getInstance().get(editFlightsCBox.getSelectedIndex()-DESCIPTION_LENGTH);
    }
    public EditFlightsView(){
        this(new EditFlightsModel());
    }
    private void createPanel(){
        layout = new GridLayout(0, 2);
        depPanel = new JPanel();
        destPanel = new JPanel();
        depD_TPanel = new JPanel();
        capacityPanel = new JPanel();
        flightPricePanel = new JPanel();
        buttonPanel = new JPanel();
        editFlightBtn = new JButton("Edit");
        
        editFlightBtn.addActionListener(new UC2AL(new UCEditFlights(this,model)));
        //set edit button defaultly disabled, is set to enabled when a flight is selected.
        editFlightBtn.setEnabled(false);
        String[] currentFlights = new String[AllFlights.getInstance().size()+DESCIPTION_LENGTH];

        DateFormat df = new SimpleDateFormat("yyyy.MM.dd");
        currentFlights[0] = "Select a flight to edit from the list:";
        currentFlights[1] = "--------------------------------------";
        for (int i = 0; i < AllFlights.getInstance().size(); i++) {
            currentFlights[i+DESCIPTION_LENGTH] = AllFlights.getInstance().get(i).toString();
        }
        editFlightsCBox = new JComboBox(currentFlights);
        editFlightsCBox.addItemListener(new EditCBoxChange());

        depCBox = new JComboBox(AllLocations.getLocationsNames());
        depLabel = new JLabel("Departure Location:");            

        depPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        depPanel.add(depLabel);
        depPanel.add(depCBox);

        destCBox = new JComboBox(AllLocations.getLocationsNames());
        destLabel = new JLabel("Destination Location:");            

        destPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        destPanel.add(destLabel);
        destPanel.add(destCBox);

        priceField = new JTextField("mmmmmmmmmm");
        priceField.setPreferredSize(priceField.getPreferredSize());
        priceField.setText("");
        priceLabel = new JLabel("Flight Price $");

        flightPricePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        flightPricePanel.add(priceLabel);
        flightPricePanel.add(priceField);

        flightContainer = new JPanel(new GridLayout(0, 1));
        flightContainer.add(new JPanel());
        flightContainer.add(flightPricePanel);

        String[] dayArray;
        String[] monthArray = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
        String[] yearArray;

        int baseYear = Calendar.getInstance().get(Calendar.YEAR);

        int years = baseYear + 50;
        int days = 31;

        yearArray = new String[years];
        dayArray = new String[days];

        for (int i= 0; i < years; i++){
            yearArray[i] = Integer.toString(baseYear + i);
        }
        for (int i= 1; i <= days; i++){
            dayArray[i-1] = Integer.toString(i);
        }
        for (int i = 0; i < 24; i++){
        }
        String[] hours = new String[24];
        String[] minutes = new String[60];
        for (int i=0; i < hours.length; i++){
            if (i<9){
                hours[i] = "0" + Integer.toString(i+1);
            }
            else {
                hours[i] = Integer.toString(i+1);
            }
        }
        for (int i=0; i < minutes.length; i++){
            if (i<9){
                minutes[i] = "0" + Integer.toString(i);
            }
            else {
                minutes[i] = Integer.toString(i);
            }
        }
        
        dayCBox = new JComboBox(dayArray);
        monthCBox = new JComboBox(monthArray);
        yearCBox = new JComboBox(yearArray);
        dhoursCBox = new JComboBox(hours);
        dminutesCBox = new JComboBox(minutes);
        ahoursCBox = new JComboBox(hours);
        aminutesCBox = new JComboBox(minutes);

        depD_TPanel.setLayout(new GridLayout(0,1));
        dateContainer = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        depTimeContainer = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        arrTPanel = new JPanel(new GridLayout(0,1));
        arrTimePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));

        dateContainer.add(new JLabel("Dep Date:"));

        dateContainer.add(dayCBox);
        dateContainer.add(monthCBox);
        dateContainer.add(yearCBox);
        depTimeContainer.add(new JLabel("Dep Time:"));
        depTimeContainer.add(dhoursCBox);
        depTimeContainer.add(new JLabel(":"));
        depTimeContainer.add(dminutesCBox);

        depD_TPanel.add(dateContainer);
        depD_TPanel.add(depTimeContainer);

        arrTimePanel.add(new JLabel("Arr Time:"));
        arrTimePanel.add(ahoursCBox);
        arrTimePanel.add(new JLabel(":"));
        arrTimePanel.add(aminutesCBox);

        arrTPanel.add(new JPanel());
        arrTPanel.add(arrTimePanel);

        capacityField = new JTextField("mmmmmmmmmm");
        capacityField.setPreferredSize(priceField.getPreferredSize());
        capacityField.setText("");
        capacityLabel = new JLabel("Plane Compacity");

        capacityPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        capacityPanel.add(capacityLabel);
        capacityPanel.add(capacityField);

        capacityContainer = new JPanel(new GridLayout(0, 1));
        capacityContainer.add(new JPanel());
        capacityContainer.add(capacityPanel);

        buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        buttonPanel.add(editFlightBtn);

        
        JPanel editPanel = new JPanel(layout);
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        add(editFlightsCBox);
        editPanel.add(depPanel);
        editPanel.add(destPanel);            
        editPanel.add(depD_TPanel);
        editPanel.add(arrTPanel);
        editPanel.add(capacityContainer);
        editPanel.add(flightContainer);
        editPanel.add(buttonPanel);
        add(editPanel);
    }
    public void clearView() {
        this.removeAll();
        createPanel();
        this.validate();
    }
    public String getDepName(){
        return (String)depCBox.getSelectedItem();
    }
    public String getDestName(){
        return (String)destCBox.getSelectedItem();
    }
    public String getDate(){
        return yearCBox.getSelectedItem()
                    +"-"+Integer.toString(monthCBox.getSelectedIndex()+1)
                    +"-"+dayCBox.getSelectedItem().toString();
    }
    public String getArrTime(){
        return ahoursCBox.getSelectedItem() + ":"
                + aminutesCBox.getSelectedItem() + ":00";
    }
    public String getDepTime(){
        return dhoursCBox.getSelectedItem() + ":"
                + dminutesCBox.getSelectedItem() + ":00";
    }
    public int getCapacity(){
        return Integer.parseInt(capacityField.getText());
    }
    public int getPrice(){
        return Integer.parseInt(priceField.getText());
    }
    private class EditCBoxChange implements ItemListener {
        public void itemStateChanged(ItemEvent e) {
            JComboBox tempCBox = (JComboBox)e.getSource();

            if (tempCBox.getSelectedIndex() > DESCIPTION_LENGTH-1) {
                editFlightBtn.setEnabled(true);
                Flight tempFlight = AllFlights.getInstance().elementAt(editFlightsCBox.getSelectedIndex()-DESCIPTION_LENGTH);

                depCBox.setSelectedItem(AllLocations.getLocationNameFromID(tempFlight.getDep_id()));
                destCBox.setSelectedItem(AllLocations.getLocationNameFromID(tempFlight.getDest_id()));

                priceField.setText(Integer.toString(tempFlight.getFlight_price()));
                capacityField.setText(Integer.toString(tempFlight.getCapacity()));

                String yearSubStr = "yyyy";
                String monthSubStr = "yyyy-mm";
                String daySubStr = "yyyy-mm-dd";
                String dmySeperatorSubStr = "-";
                String tempDate = tempFlight.getFlight_date().toString();

                yearCBox.setSelectedItem(tempDate.substring(0, yearSubStr.length()));
                
                String[] monthArray = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
                int monthNum = Integer.parseInt(tempDate.substring(yearSubStr.length()+dmySeperatorSubStr.length(),monthSubStr.length()));
                monthCBox.setSelectedItem(monthArray[monthNum-1]);
                
                dayCBox.setSelectedItem(tempDate.substring(monthSubStr.length()+dmySeperatorSubStr.length(),daySubStr.length()));

                String hoursSubStr = "hh";
                String minsSubStr = "hh:mm";
                String hmSeperatorSubStr = ":";
                String tempArrTime = tempFlight.getArr_time().toString();
                String tempDepTime = tempFlight.getDep_time().toString();

                dhoursCBox.setSelectedItem(tempDepTime.substring(0,hoursSubStr.length()));
                dminutesCBox.setSelectedItem(tempDepTime.substring(hoursSubStr.length()+hmSeperatorSubStr.length(),minsSubStr.length()));
                
                ahoursCBox.setSelectedItem(tempArrTime.substring(0,hoursSubStr.length()));
                aminutesCBox.setSelectedItem(tempArrTime.substring(hoursSubStr.length()+hmSeperatorSubStr.length(),minsSubStr.length()));
            }
            else {
                editFlightBtn.setEnabled(false);
            }
        }
    }
}

